package cours_1;

public class Couverture_Produit {

	public  int Nmc=100000;
	public int getNmc() {
		return Nmc;
	}
	public void setNmc(int nmc) {
		Nmc = nmc;
	}
	public double getDt() {
		return dt;
	}
	public void setDt(double dt) {
		this.dt = dt;
	}
	public double[] getS() {
		return S;
	}
	public void setS(double[] s) {
		S = s;
	}
	public int N=1000;
	static double  eta=0.2316419;
	static double a1=0.319381530;
	static double a2=-0.356563782;
	static double a3=1.781477937;
	static double a4=-1.821255978;
	static double a5=1.330274429;
	public double moy;
	public double B0=1;
	public double P0;
	public double A0;
	public double r=0.05;
	public double T=5;
	public double dt=0.01;
	public double K=1.5;
	public double sigma=0.5;
	public double[]S=new double[Nmc];
	public  double[] U=new double [Nmc]; 
	public  double[] P=new double [Nmc]; 
	public  double[] A=new double [Nmc]; 
	public  double[] B=new double [Nmc]; 
	public  double[] d1=new double [Nmc]; 
	public  double[] d2=new double [Nmc]; 
	public  double[] Normale=new double [Nmc];
	public Couverture_Produit() {
		for(int i=0;i<Nmc;i++){
			for(int j=0;j<N;j++){
				U[j]=Math.random();
				moy=moy+U[j];
				
		}
			moy=moy/N;
		Normale[i]=Math.sqrt(12*N)*(moy-0.5);
		moy=0;
	} 
		S[0]=1;
		for(int i=1;i<Nmc;i++){
		S[i]=S[i-1]*Math.exp((r-(Math.pow(sigma, 2)/2))*dt+Math.sqrt(dt)*sigma*Normale[i]);
		}
		for(int i=0;i<Nmc;i++){
			    d1[i]=(Math.log(S[i]/K)+(r+Math.pow(sigma, 2)/2)*(T-i*dt))/(sigma*Math.sqrt(T-i*dt));
				d2[i]=(Math.log(S[i]/K)+(r-Math.pow(sigma, 2)/2)*(T-i*dt))/(sigma*Math.sqrt(T-i*dt));
			}
		A[0]=N(d1[0]);
		B[0]=1;
		System.out.println("A0="+A[0]);
		P[0]=A[0]*S[0]+B[0];
		System.out.println("P0="+P[0]);
		for(int i=1;i<Nmc;i++){
		A[i]=N(S[i]);
		}
	for(int i=0;i<Nmc-1;i++){
		P[i+1]=A[i]*S[i+1]+(1+r*dt)*B[i];
		B[i+1]=P[i+1]-A[i+1]*S[i+1];
	}
		
		
}
	public double[] getP() {
		return P;
	}
	public void setP(double[] p) {
		P = p;
	}
	public double[] getA() {
		return A;
	}
	public void setA(double[] a) {
		A = a;
	}
	public double[] getB() {
		return B;
	}
	public void setB(double[] b) {
		B = b;
	}
	public static double Nprime(double x){
		double Np;
		 Np=(1/Math.sqrt(2*Math.PI))*Math.exp(-x*x/2);
		 return Np;
	}
	public static double Tho(double x, double eta){
		double k;
		 k=1/(1+eta*x);
		 return k;
	}
	
	public static double N(double x){
		double n;
		if (x>=0){
			n= 1-Nprime(x)*(a1*Tho(x, eta)+a2*Math.pow(Tho(x, eta),2)+a3*Math.pow(Tho(x, eta),3)+a4*Math.pow(Tho(x, eta),4)+a5*Math.pow(Tho(x, eta),5));
		}
		else{
			n=1-N(-x);
		}
		return n;
		
	}	
}